package com.blate.dogxiao.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.util.Pair;
import android.view.View;

import com.blate.dogxiao.R;
import com.blate.dogxiao.databinding.ActivityMainBinding;
import com.blate.dogxiao.fragment.DiscoveryFragment;
import com.blate.dogxiao.fragment.HomeFragment;
import com.blate.dogxiao.fragment.MessageFragment;
import com.blate.dogxiao.fragment.MineFragment;

import java.util.HashSet;
import java.util.Set;

public class MainActivity
        extends AppCompatActivity {

    private ActivityMainBinding mBinding;

    private final Set<Pair<View, Fragment>> mFragmentSet = new HashSet<>();
    private HomeFragment mHomeFragment;
    private DiscoveryFragment mDiscoveryFragment;
    private MessageFragment mMessageFragment;
    private MineFragment mMineFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        mBinding.setHost(this);
        initialize();
    }

    private void initialize() {
        loadAllPage();
        onHomeClick();
    }

    public void loadAllPage() {
        mHomeFragment = new HomeFragment();
        mFragmentSet.add(new Pair<>(mBinding.llHome, mHomeFragment));

        mDiscoveryFragment = new DiscoveryFragment();
        mFragmentSet.add(new Pair<>(mBinding.llDiscovery, mDiscoveryFragment));

        mMessageFragment = new MessageFragment();
        mFragmentSet.add(new Pair<>(mBinding.llMessage, mMessageFragment));

        mMineFragment = new MineFragment();
        mFragmentSet.add(new Pair<>(mBinding.llMine, mMineFragment));

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.fl_page_container, mHomeFragment);
        fragmentTransaction.add(R.id.fl_page_container, mDiscoveryFragment);
        fragmentTransaction.add(R.id.fl_page_container, mMessageFragment);
        fragmentTransaction.add(R.id.fl_page_container, mMineFragment);
        fragmentTransaction.commitNow();
    }

    private void showFragment(@NonNull Fragment fragment) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        for (Pair<View, Fragment> iteration : mFragmentSet) {
            if (iteration.second == fragment) {
                if (iteration.second.isAdded()) {
                    fragmentTransaction.show(iteration.second);
                } else {
                    fragmentTransaction.add(R.id.fl_page_container, iteration.second);
                }
                iteration.first.setSelected(true);
            } else {
                safeHideFragment(fragmentTransaction, iteration.second);
                iteration.first.setSelected(false);
            }
        }
        fragmentTransaction.commit();
    }

    private void safeHideFragment(FragmentTransaction fragmentTransaction, Fragment fragment) {
        if (fragment == null) {
            return;
        }
        if (!fragment.isAdded()) {
            return;
        }
        if (!fragment.isHidden()) {
            fragmentTransaction.hide(fragment);
        }
    }

    public void onHomeClick() {
        if (mHomeFragment == null) {
            mHomeFragment = new HomeFragment();
            mFragmentSet.add(new Pair<>(mBinding.llHome, mHomeFragment));
        }
        showFragment(mHomeFragment);
    }

    public void onDiscoveryClick() {
        if (mDiscoveryFragment == null) {
            mDiscoveryFragment = new DiscoveryFragment();
            mFragmentSet.add(new Pair<>(mBinding.llDiscovery, mDiscoveryFragment));
        }
        showFragment(mDiscoveryFragment);
    }

    public void onAddHouseClick() {
        startActivity(new Intent(this, ReleaseHouseActivity.class));
    }

    public void onMessageClick() {
        if (mMessageFragment == null) {
            mMessageFragment = new MessageFragment();
            mFragmentSet.add(new Pair<>(mBinding.llMessage, mMessageFragment));
        }
        showFragment(mMessageFragment);
    }

    public void onMineClick() {
        if (mMineFragment == null) {
            mMineFragment = new MineFragment();
            mFragmentSet.add(new Pair<>(mBinding.llMine, mMineFragment));
        }
        showFragment(mMineFragment);
    }

}